﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPC.DbMapper.IPCDBConnect
{
    public class DbBussiness
    {
        #region 单例模式
        private static DbBussiness _instance;
        public static DbBussiness Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DbBussiness();
                }
                return _instance;
            }
        }
        #endregion

        #region 根据通道的name获取guid
        /// <summary>
        /// 根据通道的name获取guid
        /// </summary>
        /// <param name="channelName"></param>
        /// <returns></returns>
        public string GetChannelGuiByChannelName(string channelName)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                Channel channel = iPCDbContext.Channels.Where(o=>o.ChannelName == channelName).FirstOrDefault();
                iPCDbContext.Dispose();
                return channel.Guid;
            }
            catch (Exception ex)
            {
               return string.Empty;
            }
        }
        #endregion

        #region 根据设备的name获取guid
        /// <summary>
        /// 根据设备的name获取fullguid
        /// </summary>
        /// <param name="channelName"></param>
        /// <returns></returns>
        public string GetEquipGuiByChannelGuiAndEquipName(string channelGuid,string equipName)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                Equip equip = iPCDbContext.Equips.Where(o => o.ChannelName == channelGuid && o.Name == equipName).FirstOrDefault();
                iPCDbContext.Dispose();
                return equip.EquipFullName;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        #endregion


        #region 根据组的name获取guid
        /// <summary>
        /// 根据组的name获取fullguid
        /// </summary>
        /// <param name="channelName"></param>
        /// <returns></returns>
        public string GetGroupGuiByEquipChannelGuiAndGroupName(string channelEquipGuid, string groupName)
        {
            try
            {
                if (groupName != "")
                {
                    IPCDbContext iPCDbContext = new IPCDbContext();
                    Group group = iPCDbContext.Groups.Where(o => o.EquipFullName == channelEquipGuid && o.Name == groupName).FirstOrDefault();
                    iPCDbContext.Dispose();
                    return group.GroupFullName;
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        #endregion

        #region 根据标签的name获取guid
        /// <summary>
        /// 根据组的name获取fullguid
        /// </summary>
        /// <param name="channelName"></param>
        /// <returns></returns>
        public string GetTagGuiByGuiAndTagName(string channelEquipGuid, string tagName)
        {
            try
            {
                string[] names = channelEquipGuid.Split('.');
                if (names.Count() == 3)//组下的标签
                {
                    IPCDbContext iPCDbContext = new IPCDbContext();
                    Tag tag = iPCDbContext.Tags.Where(o => o.GroupFullName == channelEquipGuid && o.Name == tagName).FirstOrDefault();
                    iPCDbContext.Dispose();
                    return tag.FullName;
                }
                else if (names.Count() == 2) //设备下的标签
                {
                    IPCDbContext iPCDbContext = new IPCDbContext();
                    Tag tag = iPCDbContext.Tags.Where(o => o.EquipFullName == channelEquipGuid && o.Name == tagName).FirstOrDefault();
                    iPCDbContext.Dispose();
                    return tag.FullName;
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        #endregion
    }
}
